﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Member.Model;
using Member.BLL;
using Member.IBLL;
using Member.Content;

namespace Member.Controllers
{
    public class PricesController : Controller
    {
        //价格表
        // GET: /Prices/
        private IPricesBLL bll = new PricesBLL();
        private int pagesize = 8;
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 信息展示
        /// </summary>
        /// <returns></returns>
        public ActionResult Load()
        {
            int pageindex = 1;
            if (Request["page"] != null)
            {
                try
                {
                    pageindex = int.Parse(Request["page"]);
                }
                catch (FormatException e)
                {
                    pageindex = 1;
                }
            }
            List<Prices> list = bll.GetModelsByPage(pagesize, pageindex, true, p => p.prices_id, p => true).ToList();
            int max = bll.maxshow(p => true);
            int pagecount = (max - 1) / pagesize + 1;//总页数
            int nextpage = pageindex >= pagecount ? pagecount : pageindex + 1;//下一页
            int prevpage = pageindex == 1 ? 1 : pageindex - 1;//上一页
            if (pageindex >= pagecount) pageindex = pagecount;
            Page pages = new Page
            {
                PageCount = pagecount,
                NextPage = nextpage,
                PrevPage = prevpage,
                PageNo = pageindex
            };
            return Json(new { page = pages, list = list });
        }
        public JsonResult Add()
        {
            string json = Request["json"].ToString();
            
            Prices p = JsonTo.Deserialize<Prices>(json);
            if (bll.Add(p))
            {
                return Json(new { msg = "添加成功！", s = "ok" });
            }
            else
            {
                return Json(new { msg = "添加失败！", s = "no" });
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        public JsonResult Del()
        {
            string json = Request["json"].ToString();
            List<Prices> list = JsonTo.JSONStringToList<Prices>(json);
            int count = 0;
            foreach (var u in list)
            {
                if (bll.delete(u))
                {
                    count++;
                }

            }
            //s表示状态，msg提示消息
            if (count == list.Count())
            {
                return Json(new { msg = count + "条数据全部删除成功啦", s = "ok" });

            }
            else
            {
                return Json(new { msg = count + "出现错误啦！只有" + count + "删除成功，还有" + (list.Count() - count) + "没有删除", s = "no" });
            }

        }

        public JsonResult Edit()
        {
            string json = Request["json"].ToString();
            Prices  p= JsonTo.Deserialize<Prices>(json);
            if (bll.Update(p))
            {
                return Json(new { msg = "编辑成功", s = "y" });
            }
            else
            {
                return Json(new { msg = "失败了，再来一次吧", s = "n" });
            }
        }
	}
}